#define INITGUID
#include "master.h"
#include <objbase.h>
#include <wmsdk.h>
#include <stdlib.h>
#include "metaparse.h"
#include "comfunct/comfunct.h"
#include "resource.h"

IWMMetadataEditor* CreateReader(const WCHAR* wszFileName)
{
     IWMMetadataEditor *pWMEdit = NULL;
     HRESULT hr = 0;
     
     hr = WMCreateEditor(&pWMEdit);
     if(FAILED(hr))
     {
         HRErrorInfo(hr);
     }
     
     pWMEdit->lpVtbl->Open(pWMEdit, wszFileName);
     
     return pWMEdit;
}

WCHAR* GetAttribute(IWMMetadataEditor* pWMEdit, const WCHAR* wszAttribute)
{
     WORD wByteCount = 0;
     WORD wStream = 0;
     WMT_ATTR_DATATYPE attType = WMT_TYPE_STRING;
     HRESULT hr = 0;
     WCHAR* wszBuffer = NULL;
     IWMHeaderInfo3* pWMRead = NULL;
     
     hr = pWMEdit->lpVtbl->QueryInterface(pWMEdit, &IID_IWMHeaderInfo3, (void**)&pWMRead);
     if(SUCCEEDED(hr))
	 {
		 hr = pWMRead->lpVtbl->GetAttributeByName(pWMRead, &wStream, wszAttribute, &attType, NULL, &wByteCount);
		 if(FAILED(hr))
		 {
			 wszBuffer = NULL;
		 }
		 else
		 {
			 wszBuffer = malloc(wByteCount);
			 pWMRead->lpVtbl->GetAttributeByName(pWMRead, &wStream, wszAttribute, &attType, (LPBYTE)wszBuffer, &wByteCount);
		 }
	 }
	 else
     {
         HRErrorInfo(hr);
         wszBuffer = NULL;
     }
     
     SAFE_RELEASE(pWMRead);
     return wszBuffer;
}

void DisplayLyrics(HWND hwnd, LPCTSTR szPlayingFile)
{
     WCHAR* wszFile = NULL;
     WCHAR* wszLyrics = NULL;
	 IWMMetadataEditor* pWMEdit = NULL;
	 HMENU hMenu = GetMenu(hwnd), hSubMenu = GetSubMenu(hMenu, CONTROL_MENU);
	 MENUITEMINFO mii = {sizeof(MENUITEMINFO), 0};
	 mii.fMask = MIIM_STATE;
	 
	 GetMenuItemInfo(hSubMenu, IDI_DISPLAY_LYRICS, MF_BYCOMMAND, &mii);
	 if(!(mii.fState & MFS_CHECKED)) return;
     
#ifndef UNICODE

     wszFile = UnicodeConversion(szPlayingFile);
     pWMEdit = CreateReader(wszFile);
	 wszLyrics = GetAttribute(pWMEdit, g_wszWMLyrics);
     FreePointer(wszFile);
     
#else

     pWMEdit = CreateReader(szPlayingFile);
	 wszLyrics = GetAttribute(pWMEdit, g_wszWMLyrics);
     
#endif
	 
	 if(wszLyrics == NULL)
	 {
		 SendMessage(GetDlgItem(hwnd, IDI_LYRICS), WM_SETTEXT, 0, (LPARAM)GetLocalText(26));
	 }
	 else 
	 {

#ifndef UNICODE

		 char* szLyrics = ASCIIConversion(wszLyrics);		 
		 SendMessage(GetDlgItem(hwnd, IDI_LYRICS), WM_SETTEXT, 0, (LPARAM)szLyrics);
		 FreePointer(szLyrics);
#else

		 SendMessage(GetDlgItem(hwnd, IDI_LYRICS), WM_SETTEXT, 0, (LPARAM)wszLyrics);

#endif

	 }
	 free(wszLyrics); 

	 pWMEdit->lpVtbl->Close(pWMEdit);
	 SAFE_RELEASE(pWMEdit);        
     return;
}

bool GetID3TagArtist(IWMMetadataEditor* pWMEdit, WCHAR** wszBuffer)
{
	WCHAR wszTags[4][11] = {L"ID3/Artist", L"ID3/TP1", L"ID3/TPE1", L"ID3/TPE2"};
	/* Tags are in ID3 version order, v1.x, v2.2, v2.3/4, v2.3/4 Album Artist as a last resort */
	int i = 0;
	for(; i < 4; ++i)
	{
		*wszBuffer = GetAttribute(pWMEdit, wszTags[i]);
		if(*wszBuffer != NULL)
		{
			break;
		}
	}
	return i < 4 ? false : true;
}

bool GetID3TagTitle(IWMMetadataEditor* pWMEdit, WCHAR** wszBuffer)
{
	WCHAR wszTags[3][10] = {L"ID3/Title", L"ID3/TT2", L"ID3/TIT2"};
	/* Tags are in ID3 version order, v1.x, v2.2, v2.3/4 */
	int i = 0;
	for(; i < 3; ++i)
	{
		*wszBuffer = GetAttribute(pWMEdit, wszTags[i]);
		if(*wszBuffer != NULL)
		{
			break;
		}
	}
	return i < 3 ? false : true;
}
